function changeLogin(){
    let login=JSON.parse(localStorage.getItem('login')||"[]")
    let iflogin=document.querySelector('.iflogin')
    if(login!=''){
       iflogin.innerHTML=`用户<a href='#'>${login.username}</a>`
       iflogin.nextSibling.innerHTML=`<a href='./cart.html'>我的购物车</a>`
    }else{
        
        alert('请先登录')
        location.href='../pages/login.html'
    }   
}

changeLogin()   
let checkall=false;
let goodlist=JSON.parse(localStorage.getItem('goodlist')||'[]')
goodlist.forEach(item=>{
    item.state=false
})
showcart(goodlist)
function showcart(goodlist){
   
    let str=goodlist.reduce((pre,cur)=>{
        return pre+=`
        <tr>
        <td><input type="checkbox" name="checkbox" ${cur.state?'checked':'false'} data-id=${cur.id}></td>
       <td class='username'> <img src=${cur.src}>   ${cur.name}</td>
       <td>${cur.price}</td>
       <td>
           <input type="button" name="" data-id=${cur.id} class="minus" value="-" >
          
       <input type="text" class="num" value=${cur.num}>
       <input type="button" data-id=${cur.id} class="plus" value="+">
       </td>
       <td class='allprice'>${(cur.num*cur.price).toFixed(2)}</td>
       <td><a href="#"  data-id=${cur.id} class='del'>删除</a></td>
       </tr>
        `
    },'')

   

    let tbody=document.querySelector('tbody')
    tbody.innerHTML=str
   
    let all=document.querySelector('.all')
    all.innerHTML=`<input type="checkbox" name="checkall" ${checkall?'checked':'false'} class="checkall">全选`
   
    plus()
    minus()
    del()
    allcheck()
    onecheck()
    
    total()
    
}

function plus(){
   let plus=document.querySelectorAll('.plus')
   plus.forEach((item,index)=>{
       item.onclick=function(){
        let index=this.getAttribute('data-id')
        console.log(goodlist);
        console.log(index);
        let id=goodlist.findIndex(item=> item.id==index)
       goodlist[id].num++
       total()
       localStorage.setItem('goodlist',JSON.stringify(goodlist))
        showcart(goodlist)
       
       }
   })
}

function minus(){
    let minus=document.querySelectorAll('.minus')
   
    minus.forEach((item,index)=>{
        item.onclick=function(){
            let index=this.getAttribute('data-id')
            let id=goodlist.findIndex(item=>item.id==index)
           if(goodlist[id].num>1){
            goodlist[id].num--  
            total()
           }
            localStorage.setItem('goodlist',JSON.stringify(goodlist))
            showcart(goodlist)
            

        }
    })
}

function del(){
    let dels=document.querySelectorAll('.del')
   dels.forEach((item,index)=>{
       item.onclick=function(){
           if(confirm('確定刪除嗎？')){
                let index=this.getAttribute('data-id')
               let id=goodlist.findIndex(item=>item.id==index)
            goodlist.splice(id,1)
            localStorage.setItem('goodlist',JSON.stringify(goodlist))
           showcart(goodlist)

           }
       }
   })
}


function onecheck(){
    let checks=document.querySelectorAll(`input[name='checkbox']`)
    checks.forEach(item=>{
        item.onclick=function(){
            let index=item.getAttribute('data-id')
            let thisgood=goodlist.filter(item=>item.id==index)[0]
            
            thisgood.state=!thisgood.state
            checkall=goodlist.every(item=>item.state==true)
            showcart(goodlist);
            total()
            
        }
    })
}
function allcheck(){
    let allcheck=document.querySelector('.checkall')

    allcheck.onclick=function(){
        checkall=!checkall;
        goodlist.forEach(item=>{
            item.state=checkall;
        })
       
        let allprice=document.querySelector('.fooright').querySelector('h4')
        if(checkall==false){
            allprice.innerHTML='0.00'
        }
        total()
        showcart(goodlist)
    }
}
function total(){
    //获取单个总价
    let allprice=document.querySelector('.fooright').querySelector('h4')
    let sum=0;
    goodlist.forEach(item=>{
        if(item.state){
           sum+=item.num*item.price;
           allprice.innerHTML=sum;
        }else{
            allprice.innerHTML=sum.toFixed(2);
        }
    })
 
}

let backdetail=document.querySelector('.backdetail')
backdetail.onclick=function(){
    location.href=document.referrer
}


let exit=document.querySelector('.exit')
exit.onclick=function(){
    if(confirm('退出登录?')){
        localStorage.removeItem('login')
        location.reload();
    }
}





//分类列表

let navList=document.querySelector('.navP').querySelectorAll('a')
for(let i=0;i<navList.length;i++){
    navList[i].setAttribute('data-id',i)
    navList[i].addEventListener('click',function(){
       let index=this.getAttribute('data-id')
       getNavList(index)
    })
}


function getNavList(id){
    axios({
        method:'get',
        url:'http://api.yuguoxy.com/api/shop/list/category',
        params:{
            categoryId:id
        },
    }).then(response=>{
        let data=response.data
        let {resultCode,resultInfo} =data;
        if(resultCode==1){
            let navList=resultInfo.list
            localStorage.setItem('navList',JSON.stringify(navList))
          location.href='../pages/navpage.html'
        }
    })
}


//搜索
let svg=document.querySelector('.svg')
svg.onclick=function(){
    let search=document.querySelector('.search')
    let val=search.value;
    if(val==''){
        alert('请输入搜索内容')
    }else{
        location.href=`../pages/find.html?keyword=${val}`
       
    }
   
    
}